import { fromJS } from 'immutable';
import * as constants from './constants'
    
const defualtState = fromJS({
    topicList: [],
    articleList: [],
    recommendList: [],
    articlePage: 1,
    showScroll: false
})

// 解耦 CHANGE_HOME_DATA 的case的内容
const changeHomeData = (state, action) => {
    return state.merge({
        topicList: fromJS(action.topicList),
        articleList: fromJS(action.articleList),
        recommendList: fromJS(action.recommendList),
    })
}

export default (state = defualtState, action) => {
    switch (action.type) {
        case constants.CHANGE_HOME_DATA:
            return changeHomeData(state, action);
    case constants.ADD_ARTICLE_LIST: 
        // 在原来的列表后面追加新的内容，使用concat方法
        return state.merge({
            articleList: state.get('articleList').concat(action.list),
            articlePage: action.nextPage
        })
    case constants.TOGGLE_SCROLL_TOP:
        return state.set('showScroll',action.show)
    default:
        return state;
    }   
}